package app.labs.LabJSF;

import app.AppInfo;
import app.area.SuperArea;
import app.form.Form;
import app.labs.CheckPoint;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.ejb.Local;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import javax.inject.Named;

@Named("form")
@SessionScoped
public class FormBean implements Serializable {
    
    private Form form;
    public FormBean() {
        form = new Form();
        xvalues = Form.getXValues();
        rvalues = Form.getRValues();
        bx = new HtmlSelectBooleanCheckbox[xvalues.size()];
        br = new HtmlSelectBooleanCheckbox[rvalues.size()];
        for (int i = 0; i < xvalues.size(); ++ i) {
            bx[i] = new HtmlSelectBooleanCheckbox();
            bx[i].setSelected(i == 0);
        }
        for (int i = 0; i < rvalues.size(); ++ i) {
            br[i] = new HtmlSelectBooleanCheckbox();
            br[i].setSelected(i == 0);
        }
        checkPointHistory = new ArrayList<CheckPoint>();
        System.out.println("create form bean");
    }
    
    public String getResource() {
        return AppInfo.resource;
    }
    public String getAuthors() {
        return AppInfo.authors;
    }
    public String getGroup() {
        return AppInfo.group;
    }
    public String getVariant() {
        return AppInfo.variant;
    }
    public Form getForm() {
        return form;
    }
    
    private List<Float> xvalues;
    private HtmlSelectBooleanCheckbox[] bx;
    public HtmlSelectBooleanCheckbox[] getBx() {
        return bx;
    }
    public void setBx(HtmlSelectBooleanCheckbox[] bx) {
        this.bx = bx;
    }
    private float getX () {
        for (int i = 0; i < bx.length; ++ i) {
            if (bx[i].isSelected()) {
                System.out.println(i);
                return xvalues.get(i).floatValue();
            }
        }
        System.out.println("all false");
        return 0;
    }
    
    private List<Float> rvalues;
    private HtmlSelectBooleanCheckbox[] br;
    public HtmlSelectBooleanCheckbox[] getBr() {
        return br;
    }
    public void setBr(HtmlSelectBooleanCheckbox[] br) {
        this.br = br;
    }
    private float getR () {
        for (int i = 0; i < br.length; ++ i) {
            if (br[i].isSelected()) {
                return rvalues.get(i).floatValue();
            }
        }
        return 0;
    }
    
    private float y;
    public float getY() {
        return y;
    }
    public void setY(float y) {
        this.y = y;
    }
    
    private List<CheckPoint> checkPointHistory;
    public void checkPoint() {
        float x = getX();
        float y = getY();
        float r = getR();        
        SuperArea superArea = new SuperArea();
        boolean contains = superArea.contains(x, y, r);
        checkPointHistory.add(new CheckPoint(x, y, r, contains));
    }
    public String getResult() {        
        StringBuilder str = new StringBuilder("");
        for (CheckPoint cp : checkPointHistory) {
            str.append("Точка ("+cp.getX()+", "+cp.getY()+") при R="+cp.getR()+" "+(cp.isContains() ? "попадание" : "промах")+"<br/>");
        }
        return str.toString();
    }
    public String getPoints() {
        StringBuilder str = new StringBuilder("<script type='text/javascript'>var points=[");
        for (int i = 0; i < checkPointHistory.size(); ++ i) {
            CheckPoint cp = checkPointHistory.get(i);
            str.append("{ x:"+cp.getX()+", y:"+cp.getY()+", r:"+cp.getR()+", type:"+(cp.isContains() ? "1" : "0")+" }");
            if (i < checkPointHistory.size() - 1) {
                str.append(",");
            }
        }
        return str.append("];</script>").toString();
    }
    
    public List<Float> getXvalues() {
        return xvalues;
    }
    public Float getYmin() {
        return Form.getYMin();
    }
    public Float getYmax() {
        return Form.getYMax();
    }
    public List<Float> getRvalues() {
        return rvalues;
    }
    
}
